#coding=utf-8
import datetime

from flask import jsonify
from jieba.analyse import extract_tags
from selenium import webdriver
import time
import re
from mysql import sqlExecute

def get_top_data():
    opt = webdriver.ChromeOptions()  # 创建浏览
    opt.add_argument("--headless")  # 隐藏浏览器
    opt.add_argument("--no-sandbox")
    driver = webdriver.Chrome(options=opt)  # 创建浏览器对象
    driver.get('https://www.weibotop.cn/2.0/')  # 打开网页
    time.sleep(2)  # 加载等待
    xpath = "//html/body/div/div[2]/div[2]/div[3]/ul/div/li/h5"
    top_text = driver.find_elements_by_xpath(xpath)
    path = "//html/body/div/div[2]/div[2]/div[3]/ul/div/li/p"
    web_hot = driver.find_elements_by_xpath(path)

    date_all = []

    for index in range(0, len(web_hot)):
        i = index + 1
        data_list = []
        #日期
        date = datetime.datetime.now().strftime('%Y-%m-%d')
        data_list.append(date)
        #微博热搜内容
        web_text = top_text[i].text
        data_list.append(web_text)
        #微博热搜指数
        hot_index = web_hot[index].text
        data_list.append(hot_index)
        date_all.append(data_list)
    return date_all

#第三步： 将数据保存到数据库中
def saveData(datalist):
    sqlDb = sqlExecute.MysqlDb()
    sql = "insert into hotsearch(date,web_text,web_hot) " \
          "values(%s,%s,%s)"

    result = sqlDb.execute_sql(sql,datalist)
    return result
    # sqlDb = sqlExecute.MysqlDb()
    # sql = "select confirm,nowConfirm,heal,dead from chinadaylist"
    # sql1 = "select count(1) from chinadaylist"
    # count = sqlDb.selectOne(sql1)
    # result = sqlDb.selectMany(sql, count[0])
    # data_list = []
    # for i in result:
    #     data_list.append(list(i))
    # return data_list

    # sqlDb = sqlExecute.MysqlDb()
    # sql = "select province_name,total_confirm from covid_province order by total_confirm desc limit 5"
    # sql1 = "select count(1) from covid_province"
    # count = sqlDb.selectOne(sql1)
    # result = sqlDb.selectMany(sql, count[0])
    # data_list = []
    # for i in result:
    #     data_list.append(list(i))
    # return data_list

    # sqlDb = sqlExecute.MysqlDb()
    # sql = "select web_text,web_hot from hotsearch order by web_hot desc limit 20"
    # result = sqlDb.selectMany(sql, 20)
    # data_list = []
    # for i in result:
    #     data_list.append(list(i))
    # return data_list

def main():
    datalist = get_top_data()
    result = saveData(datalist)
    print(result)
if __name__ == "__main__":
    main()